package com.banzhi.shop.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;


import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonHeadAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.MyShoperBean;

import com.banzhi.shop.model.CallBack.MyShoperCallBack;

import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.bumptech.glide.Glide;
import com.github.library.BaseRecyclerAdapter;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.R.id.srlayout;

//搜索店长
public class FindShoperActivity extends BaseActivity implements MyShoperCallBack, SwipeRefreshLayout.OnRefreshListener {

    /**
     * 下拉刷新
     */
    private SwipeRefreshLayout srllistview;
    private int page = 1;
    private RelativeLayout rlnodata;
    private EditText etfind;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;

   // private List<Goods_Data> Shopowners = new ArrayList<>();

    private String mShopownerCount;
    private MyShoperBean mMyShoperBean;

    List<MyShoperBean.ShopownerBean> Shopowners = new ArrayList<>();
    private LinearLayoutManager mLayoutManager;
    private BaseRecyclerAdapter<MyShoperBean.ShopownerBean> mBaseRecyclerAdapter;


    private int positon;
    private MyShoperImpl model;
    private CommonHeadAdapter<MyShoperBean.ShopownerBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_shoper);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        model = new MyShoperImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getMyShoperData(page+"",this);
    }

    private void setinits() {
        srllistview = (SwipeRefreshLayout) findViewById(srlayout);
        srllistview.setColorSchemeResources(R.color.themeColor);
        srllistview.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        rlnodata = (RelativeLayout) findViewById(R.id.rl_nodata);
        etfind = (EditText) findViewById(R.id.et_find);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void setListeners() {
        srllistview.setOnRefreshListener(this);
        etfind.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    page = 1;
                    model.getFindMyShoperData(page+"", etfind.getText().toString().trim(), FindShoperActivity.this);
                } else {
                    model.getMyShoperData(page+"", FindShoperActivity.this);
                }
            }
        });
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                if (etfind.getText().length() == 0) {
                    LoadingDialogUtils.showProgress(FindShoperActivity.this, "正在加载,请稍后~");
                    model.getMyShoperData(page+"", FindShoperActivity.this);
                } else {
                    LoadingDialogUtils.showProgress(FindShoperActivity.this, "正在加载,请稍后~");
                    model.getFindMyShoperData(page+"", etfind.getText().toString().trim(), FindShoperActivity.this);
                }
            }
        });

    }

    @Override
    public void ongetMyShoperDataSuccess(Object object) {
        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }
        mMyShoperBean = (MyShoperBean) object;
        List<MyShoperBean.ShopownerBean> shopowner = mMyShoperBean.getShopowner();
        if (page == 1) {
            Shopowners.clear();
        } else {
            if ( shopowner.size() == 0){
                return;
            }
        }
        Shopowners.clear();
        Shopowners.addAll(shopowner);

        if (Shopowners.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        LogUtils.d("Shopowners.size():",Shopowners.size()+"");
        mShopownerCount = mMyShoperBean.getShopownerCount();
        LogUtils.d("mShopownerCount",mShopownerCount);
        showView();

    }

    private void showView() {

        if (adapter == null) {
            adapter = new CommonHeadAdapter<MyShoperBean.ShopownerBean>(this, R.layout.recyclerview_item_shopers, Shopowners) {
                @Override
                public void convert(ViewHolder holder, MyShoperBean.ShopownerBean bean, int position) {
                    holder.setText(R.id.tv_nickname,bean.getNickname());
                    holder.setText(R.id.tv_time,bean.getAgenttime());
                    if (ASUtils.isCanUse(bean.getAvatar())) {
                        Glide.with(mContext).load(bean.getAvatar()).placeholder(R.mipmap.zhanweitu2).into((ImageView) holder.getView(R.id.civ));
                    }

                }

                @Override
                public void headconvert(HeaderViewHolder holder) {

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void ongetMyShoperDataError(Object object) {
        if (srllistview.isRefreshing()) {
            srllistview.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }

    @Override
    public void onRefresh() {
        page = 1;
        if (etfind.getText().length() == 0) {
            LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
            model.getMyShoperData(page+"", FindShoperActivity.this);
        } else {
            LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
            model.getFindMyShoperData(page+"", etfind.getText().toString().trim(), FindShoperActivity.this);
        }
    }
}
